package com.rupeng.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.rupeng.pojo.Chapter;
import com.rupeng.service.ChapterService;
import com.rupeng.util.AjaxResult;
import com.rupeng.util.CommonUtils;

@Controller
@RequestMapping("/chapter")
public class ChapterController {
	@Autowired
	private ChapterService chapterService;
	@RequestMapping("/list.do")
	public ModelAndView list(Long cardId) {
		Chapter chapter = new Chapter();
		chapter.setCardId(cardId);
		List<Chapter> chapters = chapterService.selectList(chapter, "seqNum asc");
		ModelAndView modelAndView = new ModelAndView("chapter/list");
		modelAndView.addObject("cardId", cardId);
		modelAndView.addObject("chapters", chapters);
		return modelAndView;
	}
	@RequestMapping("/delete.do")
	public @ResponseBody AjaxResult delete(Long id) {
		chapterService.delete(id);
		return AjaxResult.successInstance("章节删除成功");
	}
	@RequestMapping(value="/add.do",method=RequestMethod.GET)
	public ModelAndView addPage(Long cardId) {
		return new ModelAndView("chapter/add", "cardId", cardId);
	}
	@RequestMapping(value="/add.do",method=RequestMethod.POST)
	public @ResponseBody AjaxResult addSubmit(Long cardId, String name, Integer seqNum, String description) {
		if (cardId==null || seqNum==null || CommonUtils.isEmpty(name) || CommonUtils.isEmpty(description)) {
			return AjaxResult.errorInstance("表单信息不完整");
		}
		Chapter chapter = new Chapter();
		chapter.setCardId(cardId);
		chapter.setName(name);
		if (chapterService.isExisted(chapter)) {
			return AjaxResult.errorInstance("该名称已在本学习卡中存在");
		}
		chapter = new Chapter();
		chapter.setCardId(cardId);
		chapter.setSeqNum(seqNum);
		if (chapterService.isExisted(chapter)) {
			return AjaxResult.errorInstance("此序号已被占用");
		}
		chapter = new Chapter();
		chapter.setCardId(cardId);
		chapter.setName(name);
		chapter.setSeqNum(seqNum);
		chapter.setDescription(description);
		chapterService.insert(chapter);
		return AjaxResult.successInstance("添加成功");
	}
	@RequestMapping(value="/update.do",method=RequestMethod.GET)
	public ModelAndView updatePage(Long id) {
		Chapter chapter = chapterService.selectOne(id);
		return new ModelAndView("chapter/update", "chapter", chapter);
	}
	@RequestMapping(value="/update.do",method=RequestMethod.POST)
	public @ResponseBody AjaxResult updateSubmit(Long id, String name, Integer seqNum, String description) {
		if (id==null || seqNum==null || CommonUtils.isEmpty(name) || CommonUtils.isEmpty(description)) {
			return AjaxResult.errorInstance("表单信息不完整");
		}
		long cardId = chapterService.selectOne(id).getCardId();
		Chapter chapter = new Chapter();
		chapter.setName(name);
		chapter.setCardId(cardId);
		chapter = chapterService.selectOne(chapter);
		if (chapter!=null && chapter.getId()!=id) {
			return AjaxResult.errorInstance("该名称已在本学习卡中存在");
		}
		chapter = new Chapter();
		chapter.setCardId(cardId);
		chapter.setSeqNum(seqNum);
		chapter =  chapterService.selectOne(chapter);
		if (chapter!=null && chapter.getId()!=id) {
			return AjaxResult.errorInstance("此序号已被占用");
		}
		chapter = chapterService.selectOne(id);
		chapter.setName(name);
		chapter.setSeqNum(seqNum);
		chapter.setDescription(description);
		chapterService.update(chapter);
		return AjaxResult.successInstance("修改成功");
	}
}
